require 'tempfile'
module Config
#$DEBUG=true #uncomment to see pressed keys names 
#---------------- Key bindings #{{{
key_config=<<-EOF 
---
space: nextSlide
BackSpace: prevSlide
j: nextSlide
k: prevSlide
Escape: Gtk.main_quit
n: Window.new(@fileList,@filePos)
equal: zoomIn
minus: zoomOut
Left: move(-@moveStep,0)
Right: move(@moveStep,0)
Up: move(0,-@moveStep)
Down: move(0,@moveStep)
t: tag(@fileList[@filePos])
s: slideShow
r: @random=! @random
d: destroy
a: add_file
slash: find
p: puts @fileList[@filePos]
colon: win_eval
exclam: command
bracketleft: rotate!(-90)
bracketright: rotate!(90)
i: identify
EOF
Config::BINDKEYS= YAML::load(key_config)
#}}}
#--------------------- dmenu options
Config::DMENU_OPTIONS=%{-i -fn "-misc-fixed-medium-r-normal--20-200-75-75-c-100-iso8859-5"}
#--------------------- User methods
# Commands depends on shell!
def rotate!(angle) #{{{
   command("convert -rotate #{angle} \# \#")
   reload 
end
#}}}
def brightness!(arg) #{{{
 command("convert -brightness-contrast #{arg} \# \#")
 reload
end
#}}}
def contrast!(arg) #{{{
 command("convert -brightness-contrast 0x#{arg} \# \#")
 reload
end
#}}}
def identify #{{{
  command "identify #"
end
#}}}
def backup(suffix=".bac") #{{{
  command("cp # ##{suffix}")
end
#}}}
def load_backup(suffix=".bac") #{{{
  command("cp ##{suffix} #")
  reload
end
#}}}
Config::MUTT_MAILS=`sed -e 's/^alias\\s*\\([^ ]*\\).*/\\1/' -e '/^$/d' ~/.mutt_alias`
def mail_to(mail,subject,text) #{{{
    mail=`dmenu -p 'mail:' #{Config::DMENU_OPTIONS} <<EOF
#{Config::MUTT_MAILS}
EOF
    ` if mail.nil?
    return if mail==""
    command(%{mutt #{mail} -a # -s "#{subject}" <<< "#{text}"})
end
#}}}
def mail_selected(mail,subject,text) #{{{
    mail=`dmenu -p 'mail:' #{Config::DMENU_OPTIONS} <<EOF
#{Config::MUTT_MAILS}
EOF
    ` if mail.nil?
    return if mail==""
  attachment=@tags.map{|img| "-a "+img }*" "
  command(%{mutt #{mail} #{attachment} -s "#{subject}" <<< "#{text}"})
end
#}}}
Config::FILE_MASK=/^[^\.]/ # exclude hidden files and dirs
def add_file(path=".") #{{{
  if File.directory?(path)
    path=File.expand_path(path)
    dir=Dir.new(path) 
    list=dir.find.select{|file| file.match Config::FILE_MASK}
    path=`dmenu -p "#{File.basename(path)}:" #{Config::DMENU_OPTIONS} <<EOF
..
*
**
#{list*"\n"}
EOF
    ` 
    return if path==""
    add_file(dir.path+File::SEPARATOR+path)
  elsif path.match /\*\*$/
    path.gsub!(/#{File::SEPARATOR}\*\*$/,"")
    return if not File.directory?(path)
    add_file(path+File::SEPARATOR+"*")
    Dir.new(path).find.select{|dir| 
      dir!="." and dir!=".." and
      File.directory?(path+File::SEPARATOR+dir)
    }.each{|dir| add_file(path+File::SEPARATOR+dir+File::SEPARATOR+"**") }
  elsif path.match /\*$/
    path.gsub!(/#{File::SEPARATOR}\*$/,"")
    Dir.new(path).find.select{|file| 
      not File.directory?(path+File::SEPARATOR+file)
  }.each{|file| add_file(path+File::SEPARATOR+file) }
  else
    @fileList << path if not File.directory?(path)
  end
end
#}}}
def find(img=nil) #{{{
  img=`dmenu -p "find:" #{Config::DMENU_OPTIONS} <<EOF
#{@fileList*"\n"}
EOF
  ` if img.nil?
  index=@fileList.find_index(img)
  @filePos=index if not index.nil?
  reload
end
#}}}
def select(mask=nil) #{{{
  mask=`dmenu -p "mask:" #{Config::DMENU_OPTIONS} <<EOF
#{@fileList*"\n"}
EOF
  ` if mask.nil?
  return if mask==""
  @tags+=@fileList.select{|img| img.match /#{mask}/}
  @tags.uniq!
end
#}}}
def uniq #{{{
  @fileList.uniq!
end
#}}}
def mv(name) #{{{
  command "mv # '#{name}'"
  @fileList[@filePos]=name
  reload
end
#}}}
def google(query,size=:l,npics=20) #{{{
  url="http://www.google.ru/images?q=#{query}&safe=off&num=#{npics}&tbs=isch:1,isz:#{size}"
  @fileList+=`w3m -dump_source '#{url}' | sed 's/imgurl=/\\nimgurl=/g' | grep --only-matching 'imgurl=[^&]*' | sed 's/^imgurl=//'`.split("\n")
end
#}}}
def google_cache #{{{
  add_file(ENV['HOME']+File::SEPARATOR+".rview/Cache/**")
end
#}}}
def google_clean #{{{
  system "rm -rf #{ENV['HOME']+File::SEPARATOR}.rview/Cache/*"
end
#}}}
def picasa_new(album,imgs=@tags) #{{{
  imgs.each{|file| command "jpegexiforient -1 #{file}"}
  command "google picasa create #{album} #{imgs*' '}"
end
#}}}
end
